﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;

public partial class DataInterface_DataSetting : Frame.Pages.BasePage
{
    string[] MappingValueName = { "Area","ConsumePeriod","Dish","DishType","DishUnit","Tables","ConsumeType"};
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!IsPostBack)//判断是否有激发postback 在本页面中即按钮按键的postback的click事件
        {
            //构建LTS数据上下文
            using (DAL.CMSDALDataContext Dcontext = new DAL.CMSDALDataContext())
            {
                //遍历MappingValueName 数组
                foreach (string item in MappingValueName)
                {
                    //获取特定的系统设置对象（数据相关）
                    DAL.BI_SysSettings setting =
                        Dcontext.BI_SysSettings.FirstOrDefault(o => o.SettingName == string.Format("InterFaceSetting_{0}", item));
                    if (setting == null)//判断是否为空
                    {
                        //为空则构建新的对应对象 以下为快速构建实例的方法
                        setting = new DAL.BI_SysSettings()
                        {
                            SettingName = string.Format("InterFaceSetting_{0}", item),
                            Value = "0",
                            Remark = string.Format("BI_{0}表数据管理模式设置，0为人工管理，1为接口管理", item)
                        };
                        //做插入操作
                        Dcontext.BI_SysSettings.InsertOnSubmit(setting);
                        //保存数据
                        Dcontext.SubmitChanges();
                    }
                    //通过控件名称 做控件查找 也可以直接通过变量去的 如Detailpanel
                    ASPxCheckBox cb = Detailpanel.FindControl(string.Format("cb_{0}", item)) as ASPxCheckBox;
                    //判断数据状态设置控件状态
                    if (cb != null)
                    {
                        cb.Checked = (setting.Value == "1");
                    }
                }
            }
        }
    }
    protected void SubmitBtn_Click(object sender, EventArgs e)
    {
        //错误抓取操作
        try
        {
            using (DAL.CMSDALDataContext Dcontext = new DAL.CMSDALDataContext())
            {
                foreach (string item in MappingValueName)
                {
                    DAL.BI_SysSettings setting =
                        Dcontext.BI_SysSettings.FirstOrDefault(o => o.SettingName == string.Format("InterFaceSetting_{0}", item));
                    ASPxCheckBox cb = Detailpanel.FindControl(string.Format("cb_{0}", item)) as ASPxCheckBox;
                    if (cb != null)
                    {
                        setting.Value = (cb.Checked ? "1" : "0");
                    }
                }
                Dcontext.SubmitChanges();
            }
            Frame.Global.GlobalFunctions.Alert("保存成功！");
        }
        catch (Exception)
        {
            
            throw;
        }

    }


    protected override string ActionID
    {
        get { return "0"; }
    }
}